package org.example;

import org.example.model.UserEntity;
import org.example.Service.UserService;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.lang.reflect.Field;
import java.util.List;

public class MyApplicationContext {

    //配置文件路径
    private String xmlPath;

    /**
     * 加载xml配置文件
     * @param xmlPath
     */
    public MyApplicationContext(String xmlPath) throws DocumentException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {

        this.xmlPath = xmlPath;


    }

    /**
     * 获取bean
     *
     * @param beanId
     * @return
     * @throws DocumentException
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws NoSuchFieldException
     */
    public <T> T getBean(String beanId) throws DocumentException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        // 创建解析实例
        SAXReader saxReader = new SAXReader();
        // 加载xml配置文件
        Document xmlDom = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));

        //获取根节点
        Element rootElement = xmlDom.getRootElement();
        // 获取根节点下的节点列表(bean列表)
        List<Element> elements = rootElement.elements();
        for(Element bean: elements) {
            // 获取实体类的全类名
            String className = bean.attributeValue("class");


            // 创建实体的实例
            Class<?> aClass = Class.forName(className);
            Object obj = aClass.newInstance();

            //获取属性列表
            List<Element> fieldList = bean.elements();
            for (Element fieldItem : fieldList) {

                String columnName = fieldItem.attributeValue("name");

                String value = fieldItem.attributeValue("value");

                Field declaredField = aClass.getDeclaredField(columnName);

                declaredField.setAccessible(true);

                declaredField.set(obj, value);



            }

            // 获取bean的id
            String id = bean.attributeValue("id");
            if(id.equals(beanId)){
                return (T)obj;
            }
        }
        throw new RuntimeException("id不存在");
    }


    /**
     * main方法 入口
     *
     * @param args
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws DocumentException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, DocumentException, IllegalAccessException, NoSuchFieldException {

        // 初始化spring IOC
        MyApplicationContext springApplicationContext = new MyApplicationContext("application.xml");

        // 获取 user1 的实体类并初始化
        UserEntity user = springApplicationContext.getBean("user1");
        System.out.println("id:" + user.getUserId() + ",userName:" + user.getUserName());

        // 获取userService 实体类
        UserService userService = springApplicationContext.getBean("userService");
        System.out.println(userService.getNowTime());
    }
}

